package day17.tarena.GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Register implements ActionListener{
	JLabel jl1 =new JLabel("welcom");
	JTextField jt1 = new JTextField(15);
	JTextField jt2 = new JTextField(15);
	JTextField jt3 = new JTextField(15);
	JButton jb1 = new JButton("clear");
	JButton jb2 = new JButton("ok");
	private static String info;
	

	public void actionPerformed(ActionEvent e) {
		String comm = e.getActionCommand();
		if("ok".equals(comm)){
			if(jt1.getText().length()>1 || jt2.getText().length() > 1 || jt3.getText().length() >1){
			String name =jt1.getText();
			String password = jt2.getText();
			String email = jt3.getText();
			info =name+","+ password+"," + email+ ";";
			String temp = info+"\n";
		
			jt1.setText("");
			jt2.setText("");
			jt3.setText("");
			}
			try {
				FileOutputStream fos = new 
				FileOutputStream("/home/hfsd0909/register.txt",true);  //这里这个true代表追加状态
			fos.write(info.getBytes());
			fos.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
	
		}else{
			jt1.setText("");
			jt2.setText("");
			jt3.setText("");
			jt1.requestFocus();
		}
		
	}
	public Register(){
		JFrame jf = new JFrame("Register");
		GridLayout gl = new GridLayout(4,2);
		JLabel jl1 = new JLabel("name:");
		JLabel jl2 = new JLabel("password");
		JLabel jl3 = new JLabel("email");
		JPanel jp1 = new JPanel();
		jp1.add(jl1);
		jp1.add(jt1);
		JPanel jp2 = new JPanel();
		jp2.add(jl2);
		jp2.add(jt2);
		JPanel jp3 = new JPanel();
		jp3.add(jl3);
		jp3.add(jt3);
		JPanel jp4 = new JPanel();
		jp4.add(jb1);
		jp4.add(jb2);
	
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jf.setLayout(gl);
		jf.add(jp1);
		jf.add(jp2);
		jf.add(jp3);
		jf.add(jp4);
	
		jf.add(jp2,BorderLayout.SOUTH);
		jf.setLocation(300,200);
		jf.setSize(300,200);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	}
	public static void main(String[] args) throws Exception {
		new Register();

	}
}
